namespace mrs;

public class MeteorWorker : BackgroundService
{
    private readonly ILogger<MeteorWorker> _logger;
    private readonly IConfigService _configService;
    private readonly IServiceProvider _serviceProvider;

    public MeteorWorker(ILogger<MeteorWorker> logger, IConfigService configService, IServiceProvider serviceProvider)
    {
        _logger = logger;
        _configService = configService;
        _serviceProvider = serviceProvider;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("MeteorWorker service started at: {time}", DateTimeOffset.Now);

        // 使用配置中的设置
        var config = _configService.GetConfig();
        var workerSettings = config.WorkerSettings;

        // 处理 meteor 数据
        // using var scope = _serviceProvider.CreateScope();
        // var meteorService = scope.ServiceProvider.GetRequiredService<IMeteorService>();
        // await meteorService.ProcessMeteorDataAsync();

        while (!stoppingToken.IsCancellationRequested)
        {
            if (_logger.IsEnabled(LogLevel.Information))
            {
                // _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            }
            await Task.Delay(5000, stoppingToken);
        }

        _logger.LogInformation("Worker service stopped at: {time}", DateTimeOffset.Now);
    }

    public override async Task StopAsync(CancellationToken cancellationToken)
    {
        _logger.LogInformation("MeteorWorker is stopping...");
        await base.StopAsync(cancellationToken);
    }

    private async Task ProcessMeteorData(CancellationToken stoppingToken)
    {
        using var scope = _serviceProvider.CreateScope();
        var meteorService = scope.ServiceProvider.GetRequiredService<IMeteorService>();

        // 使用配置的批处理大小
        var config = _configService.GetConfig();
        var batchSize = config.WorkerSettings.BatchSize;

        // 处理数据...
        await Task.CompletedTask;
    }

    // {{END}}
}
